<?php
$isReloadable = false;
require_once("../../../pers/inc.config.php");
require_once('../../_inc/inc.session.php');
require_once('../../_inc/funct.html.php');

$sql_sel = "SELECT m.cod_modulo, m.descr as m_descr "
		.", r.* "
		." FROM wfm_regole r " 
		." LEFT JOIN wfm_moduli m ON r.id_modulo = m.id_modulo "
		." WHERE r.id_regola = ".reqVarInt('id');
$objDB->query($sql_sel,'selRec');
if(!$aRow = $objDB->fetch('selRec')) {
	backOnErr("id. errato");
}

$aVarExt['HtmlPopUpRegInp'] = "<a href=\"javascript:apriPopUpInsReg('id_regola_inp','inp');\">"
	."<img src=\"".OVF_URL_ROOT."/app/_img/popUp.png\" title=\"aggiungi una nuova regola\" border=\"0\"></a>";
$aVarExt['HtmlPopUpRegVal'] = "<a href=\"javascript:apriPopUpInsReg('id_regola_val','val');\">"
	."<img src=\"".OVF_URL_ROOT."/app/_img/popUp.png\" title=\"aggiungi una nuova regola\" border=\"0\"></a>";
$aVarExt['HtmlPopUpRegOut'] = "<a href=\"javascript:apriPopUpInsReg('id_regola_out','out');\">"
	."<img src=\"".OVF_URL_ROOT."/app/_img/popUp.png\" title=\"aggiungi una nuova regola\" border=\"0\"></a>";
	
reqDef('tipo_regola',$aRow['tipo_regola']);
reqDef('cod_regola',$aRow['cod_regola']);
reqDef('descr',$aRow['descr']);
reqDef('script',$aRow['script']);
reqDef('idm',$aRow['id_modulo']);

$aCamForm = array();
require("_inc/const.campiReg.php");

if (reqVar('azione') == 'salva') {
	$msgPag = conferma($aCamForm);
} 


$sCmdR = "";
$sCmdR .= "<a class=\"cmd1\" href=\"javascript:document.frm1.submit();\" >salva</a>";
$sCmdR .= cmdTorna('torna');

$sHtmlMod = "<table class=\"tab1\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" width=\"100%\" align='center'>\n";
$sHtmlMod .= getHtmlRigaRif();
$sHtmlMod .= "<tr>\n";
$sHtmlMod .= "<td class='lab1' colspan='3'>Modulo</td>\n";
$sHtmlMod .= "<td class='txt1' colspan='3'>".$aRow['cod_modulo']."</td>\n";
$sHtmlMod .= "<td class='txt1' colspan='14'>".$aRow['m_descr']."</td>\n";
$sHtmlMod .= "</tr>\n";
$sHtmlMod .= "</table>";

$aOpz['sottotit'] = $sHtmlMod;

testaHtml('Gestione work-flow','Modifica regola',$msgPag,'gest',$aOpz);
?>
<script language="JavaScript" type="text/javascript">
function apriPopUpInsReg(campo,tr){
  	url = "regInsPopUp.php?id=<?=reqvar('idm');?>&tr="+tr+"&campo="+campo+"&xsn=<?=XSN?>";
	window.open(url, 'nuovaPag','width=730,height=300,top=50,left=50,resizable=yes,scrollbars=yes');
}
</script>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
			<? echo $sCmdR; ?>
      </td>
   </tr>
</table>
<?=getHtmlForm('frm1','',array('enctype'=>true));?>
<?=getHtmlHidden('azione','salva');?>
<?=getHtmlHidden('id');?>
<?=getHtmlHidden('xsn',XSN);?>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
	<?=getHtmlRigaRif();?>
   <tr>
      <td class="tit1" colspan='20' align='center'>Regola</td>
   </tr>
<?
echo getHtmlCampiForm($aCamForm,'inp',$aVarExt);
?>
</table>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
			<? echo $sCmdR; ?>
      </td>
   </tr>
</table>
<?
echo "</form>\n";
codaHtml();
exit();
function conferma($aCamForm) {
	global $objDB;
	$msg = '';
	$_REQUEST['cod_regola'] = strtoupper(trim(reqVar('cod_regola')));
	if (strlen(reqVar('cod_regola'))< 3) {
		$msg .= "Codice obbligatorio di almeno 3 caratteri<br>";
	}
	if (preg_match("/[^A-Z0-9]/",reqVar('cod_regola'))) {
		$msg = "Codice '".reqVar('cod_regola')."' puo' contenere solo lettere numeri<br>";
	}
	if (trim(reqVar('descr'))=='') {
		$msg .= "Descrizione obbligatoria<br>";
	}
	
	$sqlSel = "SELECT cod_regola FROM wfm_regole  " 
			." WHERE tipo_regola = '".reqVarAS('tipo_regola')."' "
			." AND cod_regola = '".reqVarAS('cod_regola')."'"
			." AND id_modulo = ".reqVarInt('idm')
			." AND id_regola <> ".reqVarInt('id');
			$objDB->exec($sqlSel,'chkDup');
	$aRow = $objDB->fetch('chkDup');
	if ($aRow) {
		$msg .= "Codice gia' esistente<br>";
	}
	if ($msg != '') {
		return $msg;
	}
	$sSql = "UPDATE wfm_regole SET ";
	foreach($aCamForm as $rX => $aRX) {
		foreach($aRX as $jX => $aCX) {
			switch ($aCX[4]) {
				case 'date':
					$sSql .= $aCX[0]." = '".reqVarStr2YMD($aCX[0])."', ";
					break;
				default:
					$sSql .= $aCX[0]." = '".reqVarAS($aCX[0])."', ";
					break;
			}			
		}
	}
	$sSql = substr($sSql,0,-2)." WHERE id_regola = ".reqVarInt('id');
	$objDB->exec($sSql,'updRec');
	dbg_($sSql);
	header("Location: ".urlTorna(_t("regola modificata")));
	exit();
}
